


on clicked theObject
	
	
	if name of theObject is "dataBaseNameBotao" then
		display dialog "Insira o nome da Base de Dados" default answer ""
		set DBName to the text returned of the result
		set nomeDaBaseDeDadosFinal to DBName & ".db"
		set contents of text field "databaseName" of window "criarDB" to nomeDaBaseDeDadosFinal
		
		
		tell my userDefault1 to nomeDaBaseDeDados(nomeDaBaseDeDadosFinal)
		
		
		
	else if name of theObject is "dataBaseLocBotao" then
		if contents of text field "databaseName" of window "criarDB" is "" then
			display alert "Tem de primeiro dar o nome à Base de Dados!" as critical
		else
			
			tell my userDefault1 to selectThePath("dataBaseLoc", "Database Path ALIAS", "Database Path POSIX")
			
		end if
	else if name of theObject is "imagesLocBotao" then
		tell my userDefault1 to selectThePath("imagesLoc", "Imagens Obras Path ALIAS", "Imagens Obras Path POSIX")
		
	else if name of theObject is "imagesPlanosLocBotao" then
		tell my userDefault1 to selectThePath("imagesPlanoLoc", "Imagens Planos Path ALIAS", "Imagens Planos Path POSIX")
		
	else if name of theObject is "criarBaseDeDados" then
		if contents of text field "databaseName" of window "criarDB" = "" or contents of text field "databaseLoc" of window "criarDB" = "" or contents of text field "imagesLoc" of window "criarDB" = "" or contents of text field "imagesPlanoLoc" of window "criarDB" = "" then
			display alert "tem de preencher todos os campos!"
		else
			try
				tell my userDefault1 to criarUserDefaults()
				tell my BaseDeDados to criarBaseDeDados()
				display dialog "A base de dados foi criada com sucesso!" buttons {"OK"} default button 1 with icon 2
				call method "performClose:" of object window "criarDB"
			on error
				display alert "Erro. A base de dados não foi criada!"
			end try
			
		end if
		
	end if
end clicked





script userDefault1
	
	
	property defaultPlist : "com.rtech.prepress"
	property masterPlist : "Prepress Master Preferences"
	property pt : null
	
	on pathToMe()
		set pt to path to me
		set pt to POSIX path of pt & "Contents/"
		return pt
	end pathToMe
	
	on nomeDaBaseDeDados(nomeDaBaseDeDadosFinal)
		
		do shell script "defaults write " & quoted form of defaultPlist & " 'Database Name' " & "'" & nomeDaBaseDeDadosFinal & "'"
		do shell script "defaults write " & pathToMe() & quoted form of masterPlist & " 'Database Name' " & "'" & nomeDaBaseDeDadosFinal & "'"
	end nomeDaBaseDeDados
	
	
	on selectThePath(theTextField, nomeXMLTagAlias, nomeXMLTagPosix)
		
		set DBFolder to (choose folder)
		
		set filePathPOSIX to POSIX path of DBFolder
		set contents of text field theTextField of window "criarDB" to filePathPOSIX
		
		do shell script "defaults write " & quoted form of defaultPlist & " '" & nomeXMLTagAlias & "' " & "'" & DBFolder & "'"
		do shell script "defaults write " & quoted form of defaultPlist & " '" & nomeXMLTagPosix & "' " & "'" & filePathPOSIX & "'"
		
		do shell script "defaults write " & pathToMe() & quoted form of masterPlist & " '" & nomeXMLTagAlias & "' " & "'" & DBFolder & "'"
		do shell script "defaults write " & pathToMe() & quoted form of masterPlist & " '" & nomeXMLTagPosix & "' " & "'" & filePathPOSIX & "'"
		
		
	end selectThePath
	
	
	on criarUserDefaults()
		
		
		set DBName to do shell script "defaults read " & quoted form of defaultPlist & " 'Database Name' "
		set databasePathAlias to do shell script "defaults read " & quoted form of defaultPlist & " 'Database Path ALIAS' "
		set databasePathPosix to do shell script "defaults read " & quoted form of defaultPlist & " 'Database Path POSIX' "
		set imagensObrasPathAlias to do shell script "defaults read " & quoted form of defaultPlist & " 'Imagens Obras Path ALIAS' "
		set imagensObrasPathPosix to do shell script "defaults read " & quoted form of defaultPlist & " 'Imagens Obras Path POSIX' "
		set imagensPlanosPathAlias to do shell script "defaults read " & quoted form of defaultPlist & " 'Imagens Planos Path ALIAS' "
		set imagensPlanosPathPosix to do shell script "defaults read " & quoted form of defaultPlist & " 'Imagens Planos Path POSIX' "
		
		set theDatabaseFinal to databasePathPosix & DBName
		
		do shell script "defaults write " & quoted form of defaultPlist & " 'Database Name And Path' " & "'" & theDatabaseFinal & "'"
		do shell script "defaults write " & pathToMe() & quoted form of masterPlist & " 'Database Name And Path' " & "'" & theDatabaseFinal & "'"
		
		
		
		tell user defaults
			make new default entry at end of default entries with properties {name:"theDatabaseFinal", contents:theDatabaseFinal}
			make new default entry at end of default entries with properties {name:"databasePathAlias", contents:databasePathAlias}
			make new default entry at end of default entries with properties {name:"databasePathPosix", contents:databasePathPosix}
			make new default entry at end of default entries with properties {name:"imagensObrasPathAlias", contents:imagensObrasPathAlias}
			make new default entry at end of default entries with properties {name:"imagensPlanosPathAlias", contents:imagensPlanosPathAlias}
			make new default entry at end of default entries with properties {name:"imagensPlanosPathPosix", contents:imagensPlanosPathPosix}
		end tell
		
		
		
		
		
	end criarUserDefaults
	
	
	
	
end script

script BaseDeDados
	
	property defaultPlist : "com.rtech.prepress"
	property masterPlist : "Prepress Master Preferences"
	property DB : null
	property loc : null
	property head : null
	property tail : quote
	
	
	
	
	on criarBaseDeDados()
		
		set DB to do shell script "defaults read " & quoted form of defaultPlist & " 'Database Name And Path' "
		set DB to "'" & DB & "'"
		set loc to space & DB & space
		set head to "sqlite3" & loc & quote
		
		
		set tblName to "obraCabecalho"
		set newTbl to "create table " & tblName & "(numObra INTEGER PRIMARY KEY NOT NULL, trabalho TEXT, quantidade INTEGER, formatoAbertoLarg INTEGER, formatoAbertoAlt INTEGER, dataEntrada DATE, dataPrevistaSaida DATE); "
		do shell script head & newTbl & tail
		
		
		
		set tblName to "obraPrepress"
		set newTbl to "create table " & tblName & "(numObra INTEGER PRIMARY KEY NOT NULL, descricaoTrabalho TEXT, obraAnterior INTEGER, planoImposicao TEXT, popOperador TEXT, a4 INTEGER, a4Quant INTEGER, a3 INTEGER, a3Quant INTEGER, scan INTEGER, scanQuant INTEGER, clienteFornecePopUp TEXT, clienteFornecePopUpTextField TEXT); "
		do shell script head & newTbl & tail
		
		
		set tblName to "obraImpressaoPapel"
		set newTbl to "create table " & tblName & "(numObra INTEGER PRIMARY KEY NOT NULL, papelCapaTipo TEXT, papelCapaGr TEXT, papelMioloTipo TEXT, papelMioloGr TEXT, papelOutrosTipo TEXT, papelOutrosGr TEXT, papelOutrosObs TEXT, lineatura INTEGER); "
		do shell script head & newTbl & tail
		
		
		
		set tblName to "obraImpressaoCores"
		set newTbl to "create table " & tblName & "(numObra INTEGER PRIMARY KEY NOT NULL, corFrenteCapa TEXT, corVersoCapa TEXT, pantoneCapa TEXT,corFrenteMiolo TEXT, corversoMiolo TEXT, pantoneMiolo TEXT, corOutrosFrente1 TEXT, corOutrosVerso1 TEXT, pantoneOutros TEXT, corOutrosFrente2 TEXT, corOutrosVerso2 TEXT, pantoneOutros2 TEXT, corOutrosFrente3 TEXT, corOutrosVerso3 TEXT, pantoneOutros3 TEXT, corOutrosObs3 TEXT); "
		do shell script head & newTbl & tail
		
		
		
		set tblName to "obraImpressaoVerniz"
		set newTbl to "create table " & tblName & "(numObra INTEGER PRIMARY KEY NOT NULL, vernizCapa1 INTEGER, vernizCapa2 INTEGER, vernizCapa3 INTEGER, vernizCapa4 INTEGER, vernizCapa5 INTEGER, vernizCapa6 INTEGER, vernizCapa7 INTEGER, vernizCapa8 INTEGER, vernizCapa9 INTEGER, vernizCapa10 INTEGER, vernizCapa11 INTEGER, vernizCapa12 INTEGER, vernizMiolo1 INTEGER, vernizMiolo2 INTEGER, vernizMiolo3 INTEGER, vernizMiolo4 INTEGER, vernizMiolo5 INTEGER, vernizMiolo6 INTEGER, vernizMiolo7 INTEGER, vernizMiolo8 INTEGER, vernizMiolo9 INTEGER, vernizMiolo10 INTEGER, vernizMiolo11 INTEGER, vernizMiolo12 INTEGER, vernizPags Text ); "
		do shell script head & newTbl & tail
		
		set tblName to "obraImpressaoMaquinas"
		set newTbl to "create table " & tblName & "(numObra INTEGER PRIMARY KEY NOT NULL, impressaoInterna INTEGER, nomeMaqImpressaoInterna TEXT, impressaoExterna INTEGER, impressaoExternaTxt TEXT); "
		do shell script head & newTbl & tail
		
		set tblName to "obraImpressaoOutros"
		set newTbl to "create table " & tblName & "(numObra INTEGER PRIMARY KEY NOT NULL, plastMate INTEGER, plastBrilho INTEGER, estampagem INTEGER, cunho INTEGER, corteEspecial INTEGER serigrafia INTEGER, subOutrosImpressaoObs TEXT); "
		do shell script head & newTbl & tail
		
		
		
		set tblName to "obraOutros"
		set newTbl to "create table " & tblName & "(numObra INTEGER PRIMARY KEY NOT NULL, accountNome TEXT); "
		do shell script head & newTbl & tail
		
		set tblName to "clientes"
		set newTbl to "create table " & tblName & "(nomeClientes TEXT PRIMARY KEY NOT NULL); "
		do shell script head & newTbl & tail
		
		set tblName to "obraProvas"
		set newTbl to "create table " & tblName & "(numObra INTEGER PRIMARY KEY NOT NULL, pdf1 INTEGER, laser1 INTEGER, ozalide1 INTEGER, pcor1 INTEGER, env1Dia TEXT, env1Hora TEXT, rec1Dia TEXT, rec1Hora TEXT,  pdf2 INTEGER, laser2 INTEGER, ozalide2 INTEGER, pcor2 INTEGER, env2Dia TEXT, env2Hora TEXT, rec2Dia TEXT, rec2Hora TEXT,  pdf3 INTEGER, laser3 INTEGER, ozalide3 INTEGER, pcor3 INTEGER, env3Dia TEXT, env3Hora TEXT, rec3Dia TEXT, rec3Hora TEXT,  pdf4 INTEGER, laser4 INTEGER, ozalide4 INTEGER, pcor4 INTEGER, env4Dia TEXT, env4Hora TEXT, rec4Dia TEXT, rec4Hora TEXT); "
		do shell script head & newTbl & tail
		
		
		set tblName to "obraSaidaFinal"
		set newTbl to "create table " & tblName & "(numObra INTEGER PRIMARY KEY NOT NULL, dataSaidaFinal TEXT, trabalhoFinalizado INTEGER, entregueComercialData TEXT); "
		do shell script head & newTbl & tail
		
		
		set tblName to "obraImagens"
		set newTbl to "create table " & tblName & "(numObra INTEGER PRIMARY KEY NOT NULL, pathToImagemTrabalho TEXT, pathToImagem Plano TEXT, nomeImagemTrabalho TEXT, nomeImagemPlano TEXT); "
		do shell script head & newTbl & tail
		
		set tblName to "obraEntregaComercial"
		set newTbl to "create table " & tblName & "(numObra INTEGER PRIMARY KEY NOT NULL, prova1 TEXT, prova2 TEXT, prova3 TEXT, prova4 TEXT); "
		do shell script head & newTbl & tail
	end criarBaseDeDados
	
	
	
end script





